package game.games.graphictest;

import game.AbstractGame;
import game.Position;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GraphicTest extends AbstractGame
{
    private static GraphicTest instance;
    
    public synchronized static GraphicTest getInstance()
    {
        if (instance == null)
        {
            instance = new GraphicTest();
        }
        return instance;
    }
    
    private List<AbstractShape> shapes    = new ArrayList<AbstractShape>();
    
    private Color[]             colors    = new Color[] { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };
    private Color               lastColor = Color.YELLOW;
    private Random              RANDOM    = new Random(System.nanoTime());
    
    public GraphicTest()
    {
        super(false);
    }
    
    @Override
    public void render(Graphics g)
    {
        if (shapes.isEmpty())
        {
            g.setColor(Color.BLACK);
            Position size = new Position(getFrameSize());
            g.fillRect(0, 0, size.getWidth(), size.getHeight());
        }
        for (AbstractShape s : shapes)
        {
            s.render(g);
        }
    }

    @Override
    protected void doNext()
    {
        if (shapes.isEmpty())
        {
            shapes.add(new Robot());
        }
        for (AbstractShape shape : shapes)
        {
            Color newColor = colors[RANDOM.nextInt(colors.length)];
            shape.switchColors(lastColor, newColor);
            lastColor = newColor;
            Position size = new Position(getFrameSize());
            shape.setStart(new Position(RANDOM.nextInt(size.getX()), RANDOM.nextInt(size.getY())));
        }
    }
    
    public static void main(String[] args)
    {
        Thread t = new Thread(GraphicTest.getInstance(), "GraphicTest");
        t.start();
    }
}
